<?php

class products_model extends cms_model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'products';
        $this->select = array($this->table . '.*', 'store_details.value', 'maincat.title as maintitle', 'subcat.title as subtitle', 'subcat.additional', 'trades_language.title as tradetitle', 'trades.image as tradeimage', 'stores.owner_id as owner');
        $this->join = array(
            'stores' => 'stores.id = products.store_id',
            'store_details' => 'stores.id = store_details.store_id',
            'categories as maincat' => 'maincat.id = products.mcat_id',
            'categories as subcat' => 'subcat.id = products.subcat_id',
            'trades' => 'trades.id = products.trade_id',
            'trades_language' => 'trades.id = trades_language.trade_id',
        );
        $this->where = array(
            'trades_language.lang' => LANG,
            'store_details.name' => 'storename',
            'stores.active' => 'YES'
        );
    }

    function get_advanced($where = array(), $or_like = array(), $additional = array(), $keyword = array()) {
        $return = array();
        if (count($additional) > 0) {
            $this->db->join('product_additional', 'product_additional.product_id = ' . $this->table . '.id');
            $this->db->select('product_additional.value, product_additional.code');
            foreach ($additional as $k => $v) {
                if ($v != '') {
                    $this->db->where('product_additional.code', $k);
                    if (is_array($v)) {
                        foreach ($v as $vk => $vv)
                            $this->db->or_like('product_additional.value', '"' . $vv . '"');
                    } else {
                        $this->db->or_like('product_additional.value', $v);
                    }
                }
            }
        }
        if (count($where) > 0) {
            foreach ($where as $key => $value) {
                if (is_array($value)) {
                    $this->db->where_in($this->table . '.' . $key, $value);
                } else {
                    $this->db->where($this->table . '.' . $key, $value);
                }
            }

            $this->db->group_by($this->table . '.id');
            $data = $this->get();
            $index = 0;
            $ids = false;
            foreach ($data as $dt) {
                foreach ($or_like as $k => $v) {
                    if (is_array($v)) {
                        foreach ($v as $vk => $vv)
                            if (strpos($dt->$k, $vv) === FALSE) {
                                $ids[] = $index;
                                continue;
                            }
                    } else {
                        if (strpos($dt->$k, $v) === FALSE) {
                            $ids[] = $index;
                            continue;
                        }
                    }

                    if (isset($keyword['name'])) {
                        if (strpos($dt->name, $keyword['name']) === FALSE && strpos($dt->descrebtion, $keyword['name']) === FALSE && strpos($dt->details, $keyword['name']) === FALSE) {
                            $ids[] = $index;
                            continue;
                        }
                    }
                }
                $index++;
            }

            if (is_array($ids)) {
                foreach ($ids as $id)
                    unset($data[$id]);
            }
            $return = $data;
        } elseif (count($or_like) > 0 || count($keyword) > 0) {
            foreach ($or_like as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k => $v)
                        $this->db->or_like($this->table . '.' . $key, '"' . $v . '"');
                } else {
                    $this->db->or_like($this->table . '.' . $key, $value);
                }
            }
            if(count($keyword) > 0){
                foreach ($keyword as $key => $val){
                    $this->db->or_like($this->table . '.' . $key, $val);
                }
            }

            $this->db->group_by($this->table . '.id');
            $return = $this->get();
        }  else {
           $return = $this->get(); 
        }
        return $return;
    }

    function get_products($where = array()) {
        $products = $this->get($where);
        $this->load->model('product_details_model', 'prodetails');
        $nodata = array();
        $index = 0;
        foreach ($products as $product) {
            $details = $this->prodetails->get(array('product_id' => $product->id));
            if (count($details) > 0) {
                $return['prodetails'][$product->id] = $details;
            } else {
                $nodata[] = $index;
            }
            $index++;
        }

        if (count($nodata) > 0) {
            foreach ($nodata as $nd => $va)
                unset($products[$va]);
        }
        $return['products'] = $products;
        return $return;
    }

    function check_if_add($user_id = false) {
        if ($user_id) {
            $this->is_found(array('owner_id' => $user_id), array(), false, 'store');
        }
        else
            return false;
    }

}
